<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>



    <!--    屏幕自适应-->
    <script name="viewport" content="width=device-width, initial-scale=1"></script>
    <!--    引入bootstrap-->
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css">

    <!--    bootstrap依赖jQuery-->
    <script src="js/jquery.min.js"></script>
    <script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    <script src="js/http.js"></script>

</head>
<body>

<div>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">网盘</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">首页 <span class="sr-only">(current)</span></a></li>
                    <li><a href="#">施工中</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">未开发 <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Something else here</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">One more separated link</a></li>
                        </ul>
                    </li>
                </ul>
                <form class="navbar-form navbar-left">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search">
                    </div>
                    <button type="submit" class="btn btn-default">搜索</button>
                </form>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">会员中心</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">关于我们 <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">1</a></li>
                            <li><a href="#">2</a></li>
                            <li><a href="#">3</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">购买VIP</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
</div>
<form method="post" id="document1">
    <div class="jumbotron col-md-8 col-lg-offset-2">
        <div>
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    上传
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                    <li>
                        <form id="uploadFrom" method="POST" enctype="multipart/form-data">
                            <input type="file" name="document" onchange="doUpload('uploadFrom')"  multiple="multiple" value="你好"/>
                        </form>
                    </li>
                    <li>
                        <form id="uploadFromtooMuch" method="POST" enctype="multipart/form-data">
                            <input type="file" name="document" onchange="doUpload('uploadFromtooMuch')" webkitdirectory />
                        </form>
                    </li>
                </ul>
            </div>

            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    新建
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                    <li><a href="#">新建文件夹</a></li>
                    <li><a href="#">新建TXT文件</a></li>
                </ul>
            </div>

            <div class="btn-group" role="group">
                <button onclick="submiting()" type="button" class="btn btn-default" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="submitbut">
                    下载
                </button>
            </div>
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    删除
                </button>
            </div>
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    移动到
                </button>
            </div>
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    复制到
                </button>
            </div>

        </div>

        <div class="table-responsive">
            <br>
            <br>
            <table id="" class="table table-hover table-bordered" style="background-color: white">
                <thead>
                <tr>
                    <th>
                        <input type="checkbox" name=""  lay-skin="primary" lay-filter="allChoose" >
                    </th>
                    <th>文件名</th>
                    <th>大小</th>
                    <th>修改日期</th>
                </tr>
                </thead>

                <tbody id="documentList1">

                </tbody>
            </table>
        </div>
    </div>
</form>
<template id="documentList_template1">
    <tr>
        <td>
            <input type="checkbox" name="html" lay-skin="primary" lay-filter="allChoose" value="{{documentId}}" onclick="downloadDocument()">
        </td>
        <td>{{url}}</td>
        <td>{{documentSize}}</td>
        <td>{{documentDate}}</td>
    </tr>
</template>
<form method="post" id="document" >
    <button type="submit">下载</button>

</form>

</body>
</html>
<script>

    let documentId=location.search.split("?documentId=")[1];
    let md5=location.search.split("?md5=")[1];

    console.log(documentId==null);
    console.log(md5);

    if(documentId==null){
        $.ajax({
            type: "post",
            url: "http://localhost:8080/netdisc/document/show/showDocumentByMD5",
            data: {
                md5: md5,
            },
            success: function (resp) {
                let parse = JSON.parse(resp);
                console.log(parse.data.id)
                showDocument(parse)
            }
        })


    }else {
        $.ajax({
            type: "post",
            url: "http://localhost:8080/netdisc/document/show/showDocumentById",
            data: {
                documentId:documentId,
            },
            success: function (resp) {
                let parse = JSON.parse(resp);
                console.log(parse)
                showDocument(parse)

            }
        })
    }




    function showDocument(documentList) {

        let documentListHtml="";
        let url=documentList.message

        let showUrl=""

        //获取模板标签
        let documentTemplate = $("#documentList_template1").html();
        //遍历
        for (let i = 0; i < documentList.data.length; i++) {

            showUrl=url+documentList.data[i].id;
            let documentHtml="";

            //替换模板标签内容
            documentHtml = documentTemplate
                .replaceAll("{{documentId}}",documentList.data[i].id)
                .replaceAll("{{documentSize}}",documentList.data[i].size)
                .replaceAll("{{url}}","<a>"+documentList.data[i].name+"</a>")
                .replaceAll("{{documentDate}}",documentList.data[i].modifyTime);


            if(documentList.data[i].isDirectory==1){
                //替换模板标签内容
                documentHtml = documentTemplate
                    .replaceAll("{{documentId}}",documentList.data[i].id)
                    .replaceAll("{{documentSize}}",documentList.data[i].size)
                    .replaceAll("{{url}}","<a href=\""+showUrl+"\">"+documentList.data[i].name+"</a>")
                    .replaceAll("{{documentDate}}",documentList.data[i].modifyTime);

                //拼接html
            }
            //拼接html
            documentListHtml +=documentHtml;
        }
        //填充html到指定位置
        $("#documentList1").html(documentListHtml);

    }


    function downloadDocument() {
        let documentId="";
        $("input[name='html']:checked").each(function () {
            documentId +=$(this).val()+",";
        });

        console.log(documentId);
        $("#document1").attr("action", "http://localhost:8080/netdisc/document/document/download?documentId=" + documentId)

    }
    function submiting() {
	    $("#document1").submit();
    }
    
    
    /*$("#submitbut1").click(function(){

        $("#document1").submit();
    });*/

</script>